package com.zz.server.factory;

import com.zz.common.coder.GzipCodec;
import com.zz.common.handle.IdleCheckHandler;
import com.zz.server.handle.HttpChannelHandle;
import com.zz.server.handle.ServerChannelHandler;
import com.zz.common.handle.TunnelProtocolDecode;
import com.zz.common.handle.TunnelProtocolEncode;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * bootstrap 创建工厂
 *
 * @author zz
 */
public class ServerBootstrapFactory {


    /**
     * 创建server 端
     * @param serverBossGroup
     * @param serverWorkGroup
     * @return
     */
    public static ServerBootstrap createServerBootstrap(NioEventLoopGroup serverBossGroup, NioEventLoopGroup serverWorkGroup) {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(serverBossGroup, serverWorkGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel channel) throws Exception {
                // 解压缩
                channel.pipeline().addLast(new GzipCodec());
                // 自定义编解码协议
                channel.pipeline().addLast(new TunnelProtocolDecode(Integer.MAX_VALUE, 0, 4, 0, 0));
                channel.pipeline().addLast(new TunnelProtocolEncode());
                // 分片处理
                channel.pipeline().addLast(new ChunkedWriteHandler());
                //channel.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, IdleCheckHandler.WRITE_IDLE_TIME, 0));
                channel.pipeline().addLast(new ServerChannelHandler());
            }
        });
        return bootstrap;
    }

    /**
     * 创建http服务
     * @param serverBossGroup
     * @param serverWorkGroup
     * @return
     */
    public static ServerBootstrap createHttpBootstrap(NioEventLoopGroup serverBossGroup, NioEventLoopGroup serverWorkGroup) {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
        bootstrap.group(serverBossGroup, serverWorkGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel channel) throws Exception {
                channel.pipeline().addLast(new HttpServerCodec());
                channel.pipeline().addLast(new HttpObjectAggregator(8 * 1024 * 1024));
                channel.pipeline().addLast(new ChunkedWriteHandler());
                channel.pipeline().addLast(new HttpChannelHandle());
            }
        });
        return bootstrap;
    }
}
